import bean.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: zhangxupeng
 * @date:2019/5/10
 * @Email: zhangxupeng@bilibili.com
 *
 * List集合转换为Map集合
 **/
public class TestListToMap {
    public static void main(String[] args) {
        List<Person> list = Arrays.asList(
                new Person("mkyong", 30),
                new Person("jack", 20),
                new Person("lawrence", 40)
        );

        //example 1
        Map<String, Integer> result1 = list.stream().collect(
                Collectors.toMap(Person::getName, Person::getAge));

        System.out.println("Result 1 : " + result1);

        //example 2
        Map<String, Integer> result2 = list.stream().collect(
                Collectors.toMap(x -> x.getName(), x -> x.getAge()));



        System.out.println("Result 2 : " + result2);
    }

}
